/**
 * 
 */
package com.ly.hotel.util;

import org.apache.http.Header;
import org.apache.http.HttpStatus;

import android.content.Context;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestHandle;
import com.loopj.android.http.RequestParams;
import com.loopj.android.http.SyncHttpClient;
import com.loopj.android.http.TextHttpResponseHandler;
import com.ly.hotel.service.MyCloudBackService;

/**
 * @author jouson 2014-7-4
 */
public class HttpHelper {

	private static AsyncHttpClient asynClient = new AsyncHttpClient(); // 实例话对象
	private static SyncHttpClient syncHttpClient = new SyncHttpClient();
	
	private static AsyncHttpClient asynClientProxy = new AsyncHttpClient(); // 实例话对象
	private static SyncHttpClient syncHttpClientProxy = new SyncHttpClient();
	
	static {
		asynClient.setTimeout(5000); // 设置链接超时，如果不设置，默认为10s  
		syncHttpClient.setTimeout(5000);
		asynClientProxy.setTimeout(5000); // 设置链接超时，如果不设置，默认为10s
		syncHttpClientProxy.setTimeout(5000);
		
		asynClient.setUserAgent("Mozilla/5.0 (Windows NT 5.1; rc:30.0) Gccko/20100101 Firefox/30.0");
		syncHttpClient.setUserAgent("Mozilla/5.0 (Windows NT 5.1; rc:30.0) Gccko/20100101 Firefox/30.0");
		asynClientProxy.setUserAgent("Mozilla/5.0 (Windows NT 5.1; rc:30.0) Gccko/20100101 Firefox/30.0");
		syncHttpClientProxy.setUserAgent("Mozilla/5.0 (Windows NT 5.1; rc:30.0) Gccko/20100101 Firefox/30.0");
	}

	/**
	 *   
	 * @param urlString
	 * @param params
	 * @param res
	 */
	public static void getStringAsync(Context context, String urlString, RequestParams params,
			AsyncHttpResponseHandler handler) {
			asynClient.get(context, urlString, params, handler);
	}

	public static void myGetStringAsync(Context context, String urlString, RequestParams params,
			AsyncHttpResponseHandler handler,boolean isProxy) {
			if(isProxy){
				asynClientProxy.setProxy(MyCloudBackService.httpProxy, MyCloudBackService.httpProxyPort);
			}
			asynClientProxy.get(context, urlString, params, handler);
	}
	
	public static RequestHandle getBinary(Context context,String url, AsyncHttpResponseHandler hander) {
			return asynClient.get(context, url, hander);
	}
	
	public static void postAsync(Context context, String url, RequestParams params,TextHttpResponseHandler handler) {
		asynClient.post(context, url, params, handler);
	}
	
	
	/**
	 * 同步请求
	 * @param context 可以为null
	 * @param url 必填
	 * @return
	 * @throws HttpRequestFailException 
	 */
	public static String getStringSync(final Context context,String url) throws HttpRequestFailException {
		final RequestResult r = new RequestResult();
		TextHttpResponseHandler handler = new TextHttpResponseHandler() {
			
			@Override
			public void onSuccess(int status, Header[] arg1, String responseString) {
				r.content = responseString;
				r.status = status;
			}
			
			@Override
			public void onFailure(int status, Header[] arg1, String arg2, Throwable e) {
				e.printStackTrace();
				r.content = arg2;
				r.status = status;
				r.e = e;
			}
		};
		syncHttpClient.setTimeout(5000);
		syncHttpClient.get(url, handler);
		if (r.status == HttpStatus.SC_OK && (r.e == null)) {
			return r.content;
		}
		throw new HttpRequestFailException(r.e);
	}
	
	public static String getStringSync(final Context context,String url,int timeout) throws HttpRequestFailException {
		final RequestResult r = new RequestResult();
		TextHttpResponseHandler handler = new TextHttpResponseHandler() {
			
			@Override
			public void onSuccess(int status, Header[] arg1, String responseString) {
				r.content = responseString;
				r.status = status;
			}
			
			@Override
			public void onFailure(int status, Header[] arg1, String arg2, Throwable e) {
				e.printStackTrace();
				r.content = arg2;
				r.status = status;
				r.e = e;
			}
		};
		syncHttpClient.setTimeout(timeout);
		syncHttpClient.get(url, handler);
		if (r.status == HttpStatus.SC_OK && (r.e == null)) {
			return r.content;
		}
		throw new HttpRequestFailException(r.e);
	}
	
	public static String myGetStringSync(final Context context,String url,boolean isProxy) throws HttpRequestFailException {
		final RequestResult r = new RequestResult();
		TextHttpResponseHandler handler = new TextHttpResponseHandler() {
			
			@Override
			public void onSuccess(int status, Header[] arg1, String responseString) {
				r.content = responseString;
				r.status = status;
			}
			
			@Override
			public void onFailure(int status, Header[] arg1, String arg2, Throwable e) {
				e.printStackTrace();
				r.content = arg2;
				r.status = status;
				r.e = e;
			}
		};
		if(isProxy){
			syncHttpClientProxy.setProxy(MyCloudBackService.httpProxy, MyCloudBackService.httpProxyPort);
		}
		syncHttpClientProxy.get(url, handler);
		if (r.status == HttpStatus.SC_OK && (r.e == null)) {
			return r.content;
		}
		throw new HttpRequestFailException(r.e);
	}
	
	static class RequestResult {
		public int status = 0;
		public Throwable e;
		public String content;
	}
	
}
